<template>
<div class="page-component__scroll" id="content">
  <el-backtop target=".page-component__scroll ">
    <i class="el-icon-caret-top"></i>
  </el-backtop>
<!--  面包屑-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品列表</el-breadcrumb-item>
  </el-breadcrumb>
<!--  卡片区域-->
  <el-card>
    <!--   搜索区域 -->
    <el-row>
      <el-col :span="10">
        <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select" clearable @change="inputChanged" @keyup.enter.native="keyListener">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button class="padding" type="primary" @click="goAdd()">添加商品</el-button>
      </el-col>
    </el-row>
    <!--   表单区域 -->
    <el-table
        :data="goodsList"
        border
        style="width: 100%">
      <el-table-column type="index"></el-table-column>
      <el-table-column
          prop="goods_name"
          label="商品名称"
          width="500px">
      </el-table-column>
      <el-table-column
      prop="goods_price"
      label="价格（元）">
      </el-table-column>

      <el-table-column
          prop="goods_number"
          label="商品数量">
      </el-table-column>
      <el-table-column
          prop="goods_weight"
          label="商品重量">
      </el-table-column>
      <el-table-column
          label="上架时间"
      width="160px">
        <template slot-scope="scope">
          {{scope.row.add_time| dateFormat}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button class="el-icon-edit" type="primary" @click="edit(scope.row)"></el-button>
          <el-button class="el-icon-delete" type="danger" @click="deleteGoods(scope.row.goods_id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </el-card>

</div>
</template>

<script>
export default {
  name: "list",
  data(){
    return{
      // 服务器返回的商品列表数据
      goodsList:[],
      // 总共的商品数目
      total:0,
      // 查询参数
      queryInfo:{
        query:'',
        pagenum:1,
        pagesize:10
      },

    }
  },
  created() {
    this.getGoodsList();
    // 禁用滚动条
    this.RemoveScroll();
  },
  methods:{
  //  分页事件
    // 1. 当前页面显示的条数变化
    handleSizeChange(newSize){
      this.queryInfo.pagesize=newSize
      this.getGoodsList()
    },
    // 2. 当前页数发生变化
    handleCurrentChange(newPage){
      this.queryInfo.pagenum=newPage
      this.getGoodsList()
    },
    // 3. 请求服务器商品数据
   async getGoodsList(){
     const {data:res}=await this.$http.get('goods',{
      params:{
        query:this.queryInfo.query,
        pagenum:this.queryInfo.pagenum,
        pagesize:this.queryInfo.pagesize
      }
     })
     if(res.meta.status !==200){
       return this.$message.error("获取商品列表失败")
     }
     // 获取商品列表成功
     console.log(res.data.goods);
     this.goodsList=res.data.goods;
     this.total=res.data.total;
     // console.log(this.goodsList);
   },
    // 禁用滚动条
    RemoveScroll () {
      // document.body.style.overflowY = "hidden";
      // 怎样隐藏内部滚动条
      document.getElementById('content').style.overflowY = "hidden";
    },
    // 输入框内容发生改变
    inputChanged(){
      if(this.queryInfo.query===''){
        this.getGoodsList()
      }
    },
    // 键盘事件
    keyListener(){
      if(this.queryInfo.query !==''){
        this.getGoodsList();
      }
    },
    // 根据商品id删除相应商品
  async  deleteGoods(id){
      const {data:res}=await this.$http.delete(`goods/${id}`);
      if(res.meta.status !==200){
        return this.$message.error("删除商品失败")
      }
      // 删除成功
    this.getGoodsList();
    this.$message.success("删除成功")

    },
    // 点击添加商品按钮跳转页面
    goAdd(){
      this.$router.push('/goods/add');
    },
    // 修改商品
    edit(row) {
      console.log(row.goods_id);
      this.$router.push(`/edit/${row.goods_id}`)
    }


  }
}
</script>

<style scoped lang="less">
.padding{
  margin-left: 50px;
}
.page-component__scroll{
  height: 100vh;overflow-x: hidden;
}
</style>